home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / examples / section7 / ex7_4.C next >
C/C++ Source or Header  |  1992-05-16  |  3KB  |  59 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/List.h>                // Include list header file
  14. #include <cool/Gen_String.h>            // Include COOL String class
  15. #include <cool/Iterator.h>            // Include COOL Iterator class
  16. #include "paragraph.h"                // Include Stroustrup text
  17.  
  18. #include <cool/List.C>
  19. DECLARE CoolList<CoolGen_String>            // Declare CoolList type
  20. IMPLEMENT CoolList<CoolGen_String>            // Implement CoolList type
  21. IMPLEMENT CoolList_Node<CoolGen_String>            
  22.  
  23. DECLARE CoolIterator<CoolList>                // Declare CoolList iterator type
  24. IMPLEMENT CoolIterator<CoolList>            // Implement CoolList iterator type
  25.  
  26. int main (void) {
  27.   CoolList<CoolGen_String> l1;                // Declare CoolList variable
  28.   CoolGen_String s;                    // Temporary string variable
  29.   int max_count = 0;                // Temporary counting variable
  30.   cout << text;                    // Output paragraph
  31.   text.compile ("[a-zA-Z]+");            // Match any alphabetical word
  32.   while (text.find ()) {            // While still more words
  33.     text.sub_string (s, text.start (), text.end ()); // Get word from paragraph
  34.     l1.push (s);                     // And add to CoolList
  35.   }
  36.   l1.reset ();                    // Invalidate current position
  37.   while (l1.next ()) {                // While there are still nodes
  38.     int counter = 0;                // Initialize counter
  39.     CoolGen_String cur_word;            // Temporary string variable
  40.     CoolIterator<CoolList> i1 = l1.current_position ();    // Save current position
  41.     cur_word = l1.value ();            // Get word to be counted
  42.     l1.reset ();                // Invalidate current position
  43.  
  44.     while (l1.next ())                // While there are still nodes
  45.       if (l1.value () == cur_word)        // If word appears in CoolList
  46.     counter++;                // Increment usage count
  47.     if (counter > max_count) {            // If most used word so far
  48.       max_count = counter;            // Update maximum count
  49.       s = cur_word;                // And save word
  50.     }
  51.     l1.current_position () = i1;        // Restore old current position
  52.   }
  53.   cout << "There are " << l1.length () << " words\n";
  54.   l1.remove_duplicates ();            // Remove duplicate words
  55.   cout << "There are " << l1.length () << " unique words\n";
  56.   cout << "The most common word is `" << s << "' and is used " << max_count << " times\n";
  57.   return (0);                    // Exit with successful status
  58. }
  59.